package com.abbas.muyu

import android.animation.ValueAnimator
import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.MotionEvent
import android.view.View
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout

class MainActivity : AppCompatActivity(),View.OnTouchListener {

    private val container: ConstraintLayout by lazy { findViewById<ConstraintLayout>(R.id.container) }
    private val count: TextView by lazy { findViewById<TextView>(R.id.count) }
    private val muyu: ImageView by lazy { findViewById<ImageView>(R.id.muyu) }
    private val tag: TextView by lazy { findViewById<TextView>(R.id.tag) }
    private lateinit var loadAnimation: Animation
    @SuppressLint("ClickableViewAccessibility")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        container.setOnTouchListener(this)
        muyu.setOnTouchListener(this)

        loadAnimation = AnimationUtils.loadAnimation(this, R.anim.move_up)
    }

    override fun onTouch(v: View?, event: MotionEvent?): Boolean {
        when(event?.action){
            MotionEvent.ACTION_DOWN -> {
                Log.e("tag","down")
                muyu.scaleX = 0.8f
                muyu.scaleY = 0.8f
                TextView(this@MainActivity).apply {
//                    this@MainActivity.addContentView(this,ConstraintLayout.LayoutParams(width,height))
//                    val l = muyu.width + muyu.x.toInt()
//                    val t = muyu.y.toInt()
//                    val r = l + width
//                    val b = t + height
//                    layout(l , t, r ,b)
                    container.addView(this,ConstraintLayout.LayoutParams(width,height).apply { layoutParams = this })

                    loadAnimation.setAnimationListener(object :Animation.AnimationListener{
                        override fun onAnimationStart(animation: Animation?) {

                        }

                        override fun onAnimationEnd(animation: Animation?) {
                            visibility = View.GONE
                            post{
                                container.removeView(this@apply)
                            }
                        }

                        override fun onAnimationRepeat(animation: Animation?) {

                        }

                    })
                    startAnimation(loadAnimation)
                }
            }
            MotionEvent.ACTION_UP -> {
                muyu.scaleX = 1f
                muyu.scaleY = 1f
            }
        }
        return false
    }


}